<?php
/**
 * @Author: rocky
 * Date: 2019/9/29
 * Time: 17:56
 */


namespace app\shop\controller\api\v1;


use app\api\service\WechatMiniService;
use app\api\service\WechatService;
use app\common\controller\BaseApiController;


use app\common\facade\Token;
use app\shop\model\ShopOrder;
use app\shop\model\ShopOrderExpress;
use app\shop\resource\OrderListResoure;
use app\shop\resource\OrderResoure;
use app\shop\validate\OrderValidate;

use app\shop\validate\OrderConfimValidate;

/**
 * 订单
 * Class Order
 * @package app\shop\controller\api\v1
 */
class Order extends BaseApiController
{
    /*
     * 订单确认
     */
    public function postConfim()
    {
        $post = (new OrderConfimValidate())->goCheck();
        list($msg, $data) = \app\shop\service\Order::confim($post);
        if ($msg) {
            $this->errorCode(100, $msg);
        } else {
            $this->successCode($data);
        }
    }

    /*
     * 下单
     */
    public function postInfo()
    {
        $post = (new OrderValidate())->goCheck();
        $res = \app\shop\service\Order::place($post);
        if (is_array($res)) {
            $this->successCode($res);
        } else {
            $this->errorCode(100, $res);
        }
    }
    /*
     * 提醒发货
     */
    public function putRemind(){
        $order_id = $this->request->param('order_id');
        $order = ShopOrder::uid()->where('express_type',1)->where('status',2)->where('is_pay',1)->find($order_id);
        if($order){
            $order->is_remind = 1;
            $order->save();
            $this->successCode();
        }else{
            $this->errorCode(100, '订单不支持提醒');
        }
    }

    /*
     * 立即支付
     */
    public function postPay()
    {
        $order_id = $this->request->post('order_id');
        $password = $this->request->post('password');
        $order = ShopOrder::uid()->where('is_pay', 0)->where('status', 1)->find($order_id);
        if ($order) {
            if ($order['pay_type'] == 1) {
                $notify_url = url('shop_notify','',false,true);
                $data = WechatMiniService::pay(Token::getVar('openid'), $order['title'], $order['order_no'], $order['total_price'], $notify_url);
                $this->successCode($data);
            } elseif ($order['pay_type'] == 3) {
                //钱包支付
                $res = ShopOrder::pay(2, $order['order_no'], $order['total_price'], '', $password);
                if ($res === true) {
                    $this->successCode([], 201);
                } else {
                    $this->errorCode(100, $res);
                }
            }
        } else {
            $this->errorCode(100, '订单不允许支付或已支付');
        }
    }
    /*
     *  订单列表
     */
    public function getInfo(){
        $status = $this->request->get('status');
        $data = ShopOrder::uid()->pages()->when($status,function ($query) use ($status){
            if($status == 5){
                $query->where('status',3)->where('express_type',2);
            }else{
                $query->where('status',$status);
            }
        })->with(['good'])->select();

        $this->successCode(OrderListResoure::collection($data));
    }
    /*
     * 订单详情
     */
    public function getDetail(){
        $order_id = $this->request->get('order_id');
        $order = ShopOrder::uid()->get($order_id,['good','express']);
        if($order){
            $this->successCode(OrderResoure::item($order));
        }else{
            $this->errorCode(100, '订单不存在');
        }
    }
    /*
     * 查看物流
     */
    public function getExpress(){
        $express_id = $this->request->get('express_id');
        $express = ShopOrderExpress::field('number,company,mark')->find($express_id);
        $express['data'] = \library\tools\Express::query($express['mark'], $express['number']);
        $this->successCode($express);
    }
    /*
     * 确认收货
     */
    public function putConfirm(){
        $order_id = $this->request->param('order_id');
        $order = ShopOrder::uid()->where('status',3)->find($order_id);
        if($order){
            $order->status = 4;
            $order->complete_time = date('Y-m-d H:i:s');
            $order->save();
            $this->successCode();
        }else{
            $this->errorCode(100, '订单已完成或错误');
        }
    }
    /*
     * 取消订单
     */
    public function putCancel(){
        $order_id = $this->request->param('order_id');
        $order = ShopOrder::uid()->where('status',1)->where('is_pay',0)->find($order_id);
        if($order){
            $order->status = -1;
            $order->save();
            $this->successCode();
        }else{
            $this->errorCode(100, '订单已锁定或已取消');
        }
    }

}